
#ifndef FLASH_COMMON_H
#define FLASH_COMMON_H

#include "gkt_board.h"
#include "gkt_defines.h"
#include "gkt_debug.h"
#include "gkt_driver.h"
#include "gkt_flash.h"

#define FLASH_SETUP_API_L2(_TYPE)	flash_##_TYPE##_setup
#define FLASH_SETUP_API_L1(_TYPE)	FLASH_SETUP_API_L2(_TYPE)
#define FLASH_SETUP_API(_TYPE)	FLASH_SETUP_API_L1(_TYPE)

/* on-chip flash memory */
#if defined(GKT_FLASH_ONCHIP_TYPE) && GKT_FLASH_ONCHIP_PRESENT
extern void FLASH_SETUP_API(GKT_FLASH_ONCHIP_TYPE) (void);	/* declare */
#endif

/* external flash memory */
#if defined(GKT_FLASH_EXTERNAL_TYPE) && GKT_FLASH_EXTERNAL_PRESENT
extern void FLASH_SETUP_API(GKT_FLASH_EXTERNAL_TYPE) (void);	/* declare */
#endif

typedef struct _flash_device {
	uint32_t	sub_device_id;
	const gkt_flash_info_s	*info;

	void* (*map)(uint32_t id, uint32_t offset, uint32_t size);
	int (*read)(uint32_t id, uint32_t offset, uint32_t size, void *buffer);
	int (*write)(uint32_t id, uint32_t offset, uint32_t size, const void *buffer);
	int (*write_protect)(uint32_t id, uint32_t offset, uint32_t size);
	int (*erase)(uint32_t id, uint32_t offset, uint32_t size);
	int (*erase_chip)(uint32_t id);
} flash_device_s;

#define FLASH_DEVICES_DEFINE	\
	__DEVINFO_BSS flash_device_s g_flash_devices[GKT_FLASH_DEVICE_NUMS]
extern __DEVINFO_BSS flash_device_s g_flash_devices[];

#define FLASH_DEVICE(dev_id)	(&g_flash_devices[dev_id])

#endif

